<template>
  <div class="addbox">
    <div class="addbox_top">
      <i class="addbox_back" @click="backMyLocation">&lt;</i>
      <h1>添加/修改 收获地址</h1>
    </div>
    <div class="addbox_body">
      <form action="" class="addbox_body_form">
        <!-- 收货人区域 -->
        <div>
          <span>
            <label for="name">收货人
            </label>
          </span>
          <span>
            <input
            type="text"
            placeholder="名字"
            id="name"
            autocomplete="off"
            ref="name"/>
            <br>
          </span>
        </div>
        <!-- 手机区域 -->
       <div>
        <span>
          <label for="tel">手机号码</label>
        </span>
        <span>
          <input
            type="text"
            placeholder="手机号码"
            id="tel"
            autocomplete="off"
            ref="tel"/><br/>
        </span>
       </div>
        <!-- 地区设置 -->
       <div>
        <span class="pca_left">
          <label for="detail_address">所在地区</label>
        </span>
          <!-- <input type="text" placeholder="省、市、区、街道"
            id="price"
            autocomplete="off"
            ref="basicLocation"><br> -->
           <span ref="basicLocation" class="pca">
              <select name="" @change="getPname">
              <option value="">省份</option>
              <option v-for="(item,index) in pnameList" :key="index" :value="item">{{item}}</option>
            </select>
            <select name="" @change="getCname">
              <option value="">城市</option>
              <option v-for="(item,index) in cnameList" :key="index" :value="item">{{item}}</option>
            </select>
            <select name="" @change="getArea">
              <option value="">地区</option>
              <option v-for="(item,index) in areaList" :key="index" :value="item">{{item}}</option>
            </select>
          </span>
        </div>
        <div><span
          ><label for="detail_address">详细地址</label
          ><textarea
            type="textarea"
            placeholder="具体的地址"
            id="detail_address"
            autocomplete="off"
            ref="detailLocation">
            </textarea><br
        /></span></div>
        <span
          ><label for="default_address">设为默认地址</label
          ><input type="radio" class="radio"><br
        /></span>
        <span class="operate"
          ><input type="button" value="确认/修改" class="op" @click="changeLocation"/>
          <input type="reset" value="重置" class="op"
        /></span>
      </form>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { mapMutations, mapState } from 'vuex'
import request from '@/utils/request'
// import { getProvince } from '@/apis/location'
export default {
  props: {
    addLocationFlag: Boolean
  },
  data () {
    return {
      // pnameList: getProvince.data.list
      // pcityList: getCity(getProvince.data[0])
      pnameList: [],
      cnameList: [],
      areaList: [],
      pname: '',
      cname: '',
      area: ''
    }
  },
  // 获取省市区接口
  async mounted () {
    const pname = await request.get('/province')
    // console.log(res.data.list)
    this.pnameList = pname.data.list
    // .map(item => `<option value="${item}">${item}</option>`)
    // this.pnameList.unshift('<option value="">省份</option>')
    // this.pnameList.join('')
  },
  computed: {
    ...mapState(['addressLists'])
  },
  methods: {
    // 获取市区
    async getPname (e) {
      this.pname = e.target.value
      // console.log(e.target.value)
      const cname = await request.get('/city', {
        params: {
          pname: this.pname
        }
      })
      this.cnameList = cname.data.list
      const area = await request.get('/area', {
        params: {
          pname: this.pname,
          cname: this.cname
        }
      })
      this.areaList = area.data.list
    },
    async getCname (e) {
      this.cname = e.target.value
      const area = await request.get('/area', {
        params: {
          pname: this.pname,
          cname: this.cname
        }
      })
      this.areaList = area.data.list
      // console.log(e.target.value)
    },
    async getArea (e) {
      this.area = e.target.value
      // console.log(e.target.value)
    },
    ...mapMutations(['addLocation']),
    backMyLocation () {
      this.$emit('update:addLocationFlag', false)
    },
    changeLocation () {
      const changArr = {
        id: +new Date(),
        name: this.$refs.name.value,
        tel: this.$refs.tel.value,
        basicLocation: this.pname + this.cname + this.area,
        detailLocation: this.$refs.detailLocation.value
      }
      this.addLocation(changArr)
      Toast({ message: '添加/修改成功', position: top })
      this.backMyLocation()
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

.addbox {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 800px;
  height: 560px;
  background-color: white;
  color: black;
  /* text-align: center; */
  z-index: 1;
  border-radius: 4px;
  /* box-shadow: 0 5px 16px rgba(0, 0, 0, 0.8); */
  border: none;
}
.addbox_top {
  position: relative;
  margin: 0;
  padding: 0 45px 0 18px;
  height: 38px;
  line-height: 38px;
  z-index: 1;
  border-bottom: 1px solid #191919;
  border-radius: 4px 4px 0 0;
  background: #2d2d2d;
  font-weight: bold;
  font-size: 14px;
  color: #fff;
}
.addbox_back {
  position: absolute;
  left: 15px;
  z-index: 20;
  width: 20px;
  height: 20px;
  color: white;
  /* text-indent: -9999px; */
  font-style: normal;
  text-align: left;
  font-size: inherit;
  cursor: pointer;
}
.addbox_top h1 {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  margin: 0 auto;
  height: 38px;
  line-height: 38px;
  color: white;
  font-size: inherit;
  /* background-color: pink; */
}
.addbox_body {
  position: relative;
  margin: 0;
  width: 800px;
  height: 500px;
  text-align: center;
  z-index: 10;
  border-radius: 4px 4px 0 0;
  font-weight: bold;
  font-size: 14px;
  color: black;
}
.addbox_body .addbox_body_form {
  width: 100%;
  height: 100%
}
.addbox_body div {
    margin: 25px 0 0 15px;
}
.addbox_body span {
  display: inline-block;
}
.addbox_body label {
    display: inline-block;
    width: 100px;
    height: 40px;
    line-height: 40px;
    margin-left: -50px;
    /* background-color: pink; */
}
.addbox_body .pca_left {
  margin-right: 270px
}
.addbox_body .pca {
  position: absolute;
  top: 25%;
  left: 36%;
  width: 430px;
  height: 40px;
  /* background-color: red */
}
.addbox_body select {
  float:left;
  /* display: inline-block; */
  white: 150px;
  height: 40px;
  margin-left:5px;
  border-radius: 8px;
  border: none;
  font-style: normal;
  text-align: left;
  font-weight: bold;
  font-size: 14px;
  cursor: pointer;
  background-color: #e8e8e8;
}
.addbox_body textarea {
    width: 240px;
    height: 100px;
    padding: 10px 15px;
    border: none;
    border-radius: 8px;
    background-color: #e8e8e8;
    text-align: left;
    outline: none;
    resize: none;
    vertical-align: top;
}
.addbox_body input {
    width: 260px;
    height: 40px;
    /* margin: 20px 0 0 15px; */
    padding-left: 15px;
    outline: none;
    border: none;
    border-radius: 8px;
    background-color: #e8e8e8;
}
.addbox_body .radio {
     width: 20px;
     height: 20px;
     vertical-align: middle;
}

.addbox_body .operate {
    display: block;
    margin-left: -50px;
}
.addbox_body .op {
    border: 0;
    width: 100px;
    height: 50px;
    background: none;
    /* margin: 20px auto; */
    text-align: center;
    border: none;
    background-color: #e8e8e8;
    border-radius: 25px;
    outline: none;
    color: black;
    cursor: pointer;
    padding: 0;
    margin-left: 45px;
}

.addbox_body .op:hover {
    /* width: 60px;
    height: 50px; */
    width: 100px;
    height: 50px;
    color: white;
    background-color: #fc4b5d;
    border: none;
}
</style>
